<?php
/*
whmtools - easily find accounts on cpanels servers
Copyright (C) 2006  Matthew Davis <matthew@familycampground.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

require 'config.php';

process_localaccts();
process_remoteaccts();

function fetch_local_servers() {
  $query = mysql_query("SELECT hostname, ip from servers where local_remote = '0' ORDER BY hostname");
  while($row = mysql_fetch_array($query)) {
    $return[] = $row;
  }
  return $return;
}

function fetch_remote_servers() {
  $query = mysql_query("SELECT hostname, ip, username, hash from servers where local_remote = '1' ORDER BY hostname");
  while($row = mysql_fetch_array($query)) {
    $return[] = $row;
  }
  return $return;
}

function process_localaccts() {
  $servers_list = fetch_local_servers();
  global $data_file_path;
  foreach ($servers_list as $servers) {
    print $servers[0] . " - ";
    # pull in the cpanel rootacct file
    $root_accounts = file("$data_file_path/$servers[0]-rootaccts");
#    print count($root_accounts) . "<br>\n";
    if ( count($root_accounts) < 35 ) {
      print "<a href=\"https://$servers[1]:2087/scripts2/listaccts?viewall=1\">Update data</a> - " . count($root_accounts) . "<br>\n";
    } else {
      print count($root_accounts) . "<br>\n";
    }

    # clear accounts from table for the current server
    $sql = "DELETE FROM accounts where server = '$servers[0]'";
    run_sql_call($sql);
    $server_id = get_server_id($servers[0]);
    $sql = "DELETE FROM domains where server_id = '$server_id'";
    run_sql_call($sql);

    foreach ($root_accounts as $accts) {
      $split_accounts = split(",", $accts);
      list(,$domain,$ip,$account,,,,,,,,,$owner,,$skin,,$quota,,,,$package,,,,,,,,$created) = $split_accounts;
      $sql = "INSERT INTO `accounts` (`server`,`account`,`domain`,`package`,`owner`,`realIP`,`skin`,`quota`,`dateCreated`,`actualIP`) VALUES ('$servers[0]','$account','$domain','$package','$owner','$ip','$skin','$quota','$created','$ip')";
      run_sql_call($sql);
      $sql = "INSERT INTO `domains` (`server_id`, `account`, `domain`) VALUES ('$server_id', '$account', '$domain')";
      run_sql_call($sql);
    }
    process_userdomains($servers[0]);
  }
}

function process_remoteaccts() {
  $servers_list = fetch_remote_servers();
  foreach ($servers_list as $servers) {

    $sql = "DELETE FROM accounts where server = '$servers[0]'";
    run_sql_call($sql);
    $server_id = get_server_id($servers[0]);
    $sql = "DELETE FROM domains where server_id = '$server_id'";
    run_sql_call($sql);

    print $servers['hostname'] . " - ";
    $result = whmreq("/scripts/fetchcsv",$servers['ip'],$servers['username'],$servers['hash'],1);
    $accts = split("\n",$result);
    print count($accts) . "<br>\n";
    foreach ($accts as $acct) {
      $data = split(",",$acct);
      list(,$domain,$ip,$account,,,,,,,,,$owner,,$skin,,$quota,,,,$package,,,,,,,,$created) = $data;
      if ($domain != null) {
        $sql = "INSERT INTO `accounts` (`server`,`account`,`domain`,`package`,`owner`,`realIP`,`skin`,`quota`,`dateCreated`,`actualIP`) VALUES ('$servers[0]','$account','$domain','$package','$owner','$ip','$skin','$quota','$created','$ip')";
        run_sql_call($sql);
        $sql = "INSERT INTO `domains` (`server_id`, `account`, `domain`) VALUES ('$server_id', '$account', '$domain')";
        run_sql_call($sql);
      }
    }
  }
}

function run_sql_call($sql) {
  $result  = mysql_query($sql);
  if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $sql;
    die($message);
  }
}

function process_userdomains($server) {
  global $data_file_path;
  $userdomains = file("$data_file_path/$server-userdomains");
  $server_id = get_server_id($server);
  foreach ($userdomains as $domains) {
    $split_domains = split(":",$domains);
    list($domain,$account) = $split_domains;
    # so we don't add the line
    # *: nobody
    if ($domain != "*") {
      $sql = "INSERT INDO `domains` (`server_id, `account`, `domain`) VALUES ('$server_id', $account', '$domain')";
      mysql_query($sql);
    }
  }
}

function get_server_id($server) {
  $sql = "SELECT id from `servers` where hostname = '$server'";
  $result = mysql_query($sql);;
  if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $sql;
    die($message);
  }
  $server_id = mysql_fetch_array($result);
  return $server_id[0];
}

?>
